#include "mainscene.h"
#include "ui_mainscene.h"

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
      , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    // 窗体设置
    this->setFixedSize(480, 788);
    this->setWindowIcon(QIcon(":/image/res/Coin0001.png"));
    this->setWindowTitle("CoinFlip");

    connect(ui->actionQuit, &QAction::triggered, this, &MainScene::close);

    // 开始按钮音效
    QSound *startSound = new QSound(":/sound/res/TapButtonSound.wav", this);

    // 开始按钮
    MyPushButton *startBtn = new MyPushButton(":/image/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width() * 0.5 - startBtn->width() * 0.5, this->height() * 0.7);

    chooseScene = new ChooseLevelScene;

    connect(startBtn, &MyPushButton::clicked, this, [=]() {
        startSound->play();
        startBtn->Bouncing();

        // 延时进入选择关卡场景
        QTimer::singleShot(200, this, [=]() {
            this->hide();
            chooseScene->setGeometry(this->geometry());
            chooseScene->show();
        });
    });

    connect(chooseScene, &ChooseLevelScene::BackToMenu, this, [=]() {
        QTimer::singleShot(100, this, [=]() {
            chooseScene->hide();
            this->setGeometry(chooseScene->geometry());
            this->show();
        });
    });
}

MainScene::~MainScene()
{
    delete ui;
}

void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pic;
    pic.load(":/image/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pic);

    pic.load(":/image/res/Title.png");
    pic = pic.scaled(pic.width() * 0.5, pic.height() * 0.5);
    painter.drawPixmap(10, 30, pic.width(), pic.height(), pic);
}
